<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>医生工作台 - 医院排队叫号系统</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .queue-card {
            margin-bottom: 15px;
        }
        .status-badge {
            font-size: 0.9em;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="#">医生工作台</a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="../">返回首页</a>
                    </li>
                </ul>
                <span class="navbar-text" id="doctorInfo">
                    <!-- 医生信息将通过JavaScript动态填充 -->
                </span>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <!-- 当前就诊 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">当前就诊</h5>
                    </div>
                    <div class="card-body" id="currentPatient">
                        <div class="text-center py-4">
                            暂无就诊患者
                        </div>
                    </div>
                </div>
            </div>

            <!-- 等待队列 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">等待队列</h5>
                    </div>
                    <div class="card-body">
                        <div id="waitingList">
                            <!-- 等待队列将通过JavaScript动态填充 -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function() {
            let doctorId = 1; // 这里应该从登录信息中获取

            // 加载医生信息
            function loadDoctorInfo() {
                $.get(`../api/doctors/\${doctorId}`, function(doctor) {
                    $('#doctorInfo').html(`\${doctor.name} - \${doctor.department.name} - \${doctor.title}`);
                });
            }

            // 更新当前就诊患者
            function updateCurrentPatient() {
                $.get(`../api/queue/current/\${doctorId}`, function(current) {
                    if (current) {
                        $('#currentPatient').html(`
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h3>\${current.queueNumber}</h3>
                                <span class="badge bg-success">就诊中</span>
                            </div>
                            <p><strong>患者姓名：</strong>\${current.patientName}</p>
                            <p><strong>联系电话：</strong>\${current.patientPhone}</p>
                            <p><strong>等待时间：</strong>\${current.waitingTime}</p>
                            <div class="d-flex gap-2">
                                <button class="btn btn-success" onclick="completeQueue('\${current.id}')">完成就诊</button>
                                <button class="btn btn-danger" onclick="cancelQueue('\${current.id}')">取消就诊</button>
                            </div>
                        `);
                    } else {
                        $('#currentPatient').html(`
                            <div class="text-center py-4">
                                暂无就诊患者
                                <br><br>
                                <button class="btn btn-primary" onclick="callNext()">叫号下一位</button>
                            </div>
                        `);
                    }
                });
            }

            // 更新等待队列
            function updateWaitingList() {
                $.get(`../api/queue/waiting/\${doctorId}`, function(waiting) {
                    if (waiting && waiting.length > 0) {
                        let html = '';
                        waiting.forEach(function(queue) {
                            html += `
                                <div class="card queue-card">
                                    <div class="card-body">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <h5 class="card-title mb-0">\${queue.queueNumber}</h5>
                                            <span class="badge bg-warning status-badge">等待中</span>
                                        </div>
                                        <p class="card-text mt-2 mb-0">
                                            <strong>患者：</strong>\${queue.patientName}<br>
                                            <strong>等待时间：</strong>\${queue.waitingTime}
                                        </p>
                                    </div>
                                </div>
                            `;
                        });
                        $('#waitingList').html(html);
                    } else {
                        $('#waitingList').html('<div class="text-center py-4">暂无等待患者</div>');
                    }
                });
            }

            // 叫号下一位
            window.callNext = function() {
                $.post(`../api/queue/call/\${doctorId}`, function(response) {
                    if (response.success) {
                        updateCurrentPatient();
                        updateWaitingList();
                    } else {
                        alert('叫号失败：' + response.message);
                    }
                });
            };

            // 完成就诊
            window.completeQueue = function(queueId) {
                $.post(`../api/queue/complete/\${queueId}`, function(response) {
                    if (response.success) {
                        updateCurrentPatient();
                        updateWaitingList();
                    } else {
                        alert('操作失败：' + response.message);
                    }
                });
            };

            // 取消就诊
            window.cancelQueue = function(queueId) {
                if (confirm('确定要取消该患者的就诊吗？')) {
                    $.post(`../api/queue/cancel/\${queueId}`, function(response) {
                        if (response.success) {
                            updateCurrentPatient();
                            updateWaitingList();
                        } else {
                            alert('操作失败：' + response.message);
                        }
                    });
                }
            };

            // 初始化
            loadDoctorInfo();
            updateCurrentPatient();
            updateWaitingList();
            // 定期更新
            setInterval(function() {
                updateCurrentPatient();
                updateWaitingList();
            }, 5000);
        });
    </script>
</body>
</html>